﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Classes;
using Utility;

namespace OfficeManager_v0._1
{
    public partial class ViewDatabase : Form
    {
        private Database mainDatabase;
        private const string fileFilter = "Database Files(*.xml)|*.xml";
        private const string departmentCode = "dep";
        private const string officeCode = "off";
        private const string projectCode = "proj";
        private const string othersCode = "oth";
        private const string workerCode = "wor";
        private const string jobHistoryCode = "jh";
     
        public ViewDatabase()
        {
            InitializeComponent();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }

        private void fIleToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void DoIndexingRelocation()
        {
            Organization currentOrganization = mainDatabase.mainOrganization;

            if (currentOrganization.Departments != null && currentOrganization.Departments.Count != 0)
                UniqueID.department = currentOrganization.Departments[currentOrganization.Departments.Count - 1].ID;
            if (currentOrganization.Offices != null && currentOrganization.Offices.Count != 0)
                UniqueID.office = currentOrganization.Offices[currentOrganization.Offices.Count - 1].ID;
            if (currentOrganization.Projects != null && currentOrganization.Projects.Count != 0)
                UniqueID.project = currentOrganization.Projects[currentOrganization.Projects.Count - 1].ID;
            if (currentOrganization.Workers != null && currentOrganization.Workers.Count != 0)
                UniqueID.worker = currentOrganization.Workers[currentOrganization.Workers.Count - 1].ID;
        }

        private void PopulateTreeView()
        {
            Organization currentOrganization = mainDatabase.mainOrganization;
            
            MainView.Nodes.Clear();
            TreeNode mainNode = new TreeNode("Organization");
            mainNode.Name = othersCode;
            mainNode.Expand();

            #region Departments
            TreeNode departmentNode = mainNode.Nodes.Add("Departments");
            departmentNode.Expand();
            departmentNode.Name = othersCode;
            departmentNode.ContextMenuStrip = new ContextMenuStrip();

            ToolStripItem addDepartment = departmentNode.ContextMenuStrip.Items.Add("Add department");
            addDepartment.Click += (o, e) =>
                {
                    var dialog = new AddOrEditDepartment(mainDatabase.mainOrganization, false);
                    dialog.ShowDialog();

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };



            foreach (Department department in currentOrganization.Departments)
            {
                TreeNode cDepartmentNode = departmentNode.Nodes.Add(department.Name.ToString());
                cDepartmentNode.ContextMenuStrip = new ContextMenuStrip();
                cDepartmentNode.Name = departmentCode;
                cDepartmentNode.Tag = (object)department;

                ToolStripItem editDepartment = cDepartmentNode.ContextMenuStrip.Items.Add("Edit department");
                editDepartment.Tag = (object)department;

                editDepartment.Click += (o, e) =>
                    {
                        Department target = (Department)((ToolStripItem)o).Tag;
                        var dialog = new AddOrEditDepartment(mainDatabase.mainOrganization, true, target);
                        
                        dialog.ShowDialog();
                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };


                ToolStripItem removeDepartment = cDepartmentNode.ContextMenuStrip.Items.Add("Remove department");
                removeDepartment.Tag = (object)department;

                removeDepartment.Click += (o, e) =>
                {
                    Department target = (Department)((ToolStripItem)o).Tag;
                    for (int i = 0; i < target.Workers.Count; ++ i )
                    {
                        target.Workers[i].UnassignWorker();
                    }
                    foreach (Office office in target.Offices)
                    {
                        office.HostDepartment = null;
                    }
                    foreach (Project project in target.Projects)
                    {
                        project.Department = null;
                    }
                    //target.Manager.UnassignWorker();
                    mainDatabase.mainOrganization.Departments.Remove(target);

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

                #region department projects

                TreeNode cProjectsNode = cDepartmentNode.Nodes.Add("Projects");
                cProjectsNode.Name = othersCode;
                cProjectsNode.ContextMenuStrip = new ContextMenuStrip();

                ToolStripItem assignProject = cProjectsNode.ContextMenuStrip.Items.Add("Assign project");
                assignProject.Tag = (object)department;

                assignProject.Click += (o, e) =>
                    {
                        Department target = (Department)((ToolStripItem)o).Tag;
                        var dialog = new AssignProject(mainDatabase.mainOrganization, target);

                        dialog.ShowDialog();
                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };

                foreach (Project project in department.Projects)
                {
                    TreeNode cProjectNode = cProjectsNode.Nodes.Add(project.Subject);
                    cProjectNode.ContextMenuStrip = new ContextMenuStrip();
                    cProjectNode.Name = projectCode;
                    cProjectNode.Tag = (object)project;

                    ToolStripItem unassignProject = cProjectNode.ContextMenuStrip.Items.Add("Unassign project");
                    unassignProject.Tag = (object)project;

                    unassignProject.Click += (o, e) =>
                        {
                            Project target = (Project)((ToolStripItem)o).Tag;
                            target.Department.Projects.Remove(target);
                            target.Department = null;

                            Dictionary<string, bool> state = SaveTreeState(MainView);
                            PopulateTreeView();
                            RestoreTreeState(MainView, state);
                        };
                }

                #endregion

                #region department offices

                TreeNode cOfficesNode = cDepartmentNode.Nodes.Add("Offices");
                cOfficesNode.Name = othersCode;
                cOfficesNode.ContextMenuStrip = new ContextMenuStrip();

                ToolStripItem assignOffice = cOfficesNode.ContextMenuStrip.Items.Add("Assign office");
                assignOffice.Tag = (object)department;

                assignOffice.Click += (o, e) =>
                    {
                        Department target = (Department)((ToolStripItem)o).Tag;
                        var dialog = new AssignOffice(mainDatabase.mainOrganization, target);

                        dialog.ShowDialog();
                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };

                foreach (Office office in department.Offices)
                {
                    TreeNode cOfficeNode = cOfficesNode.Nodes.Add(office.Name.ToString());
                    cOfficeNode.ContextMenuStrip = new ContextMenuStrip();
                    cOfficeNode.Tag = (object)office;
                    cOfficeNode.Name = officeCode;

                    ToolStripItem unassignProject = cOfficeNode.ContextMenuStrip.Items.Add("Unassign office");
                    unassignProject.Tag = (object)office;

                    unassignProject.Click += (o, e) =>
                    {
                        Office target = (Office)((ToolStripItem)o).Tag;
                        target.HostDepartment.Offices.Remove(target);
                        target.HostDepartment = null;

                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };
                }

                #endregion

                #region department workers

                TreeNode cWorkersNode = cDepartmentNode.Nodes.Add("Workers");
                cWorkersNode.Name = othersCode;

                foreach (Worker worker in department.Workers)
                {
                    TreeNode cWorkerNode = cWorkersNode.Nodes.Add(worker.Name);
                    cWorkerNode.ContextMenuStrip = new ContextMenuStrip();
                    cWorkerNode.Name = workerCode;
                    cWorkerNode.Tag = (object)worker;

                    ToolStripItem unassignWorker = cWorkerNode.ContextMenuStrip.Items.Add("Unassign worker");
                    unassignWorker.Tag = (object)worker;

                    unassignWorker.Click += (o, e) =>
                        {
                            Worker target = (Worker)((ToolStripItem)o).Tag;
                            target.UnassignWorker();

                            Dictionary<string, bool> state = SaveTreeState(MainView);
                            PopulateTreeView();
                            RestoreTreeState(MainView, state);
                        };
                }

                #endregion
            }

            #endregion

            #region Projects
            TreeNode projectsNode = mainNode.Nodes.Add("Projects");
            projectsNode.Expand();
            projectsNode.ContextMenuStrip = new ContextMenuStrip();
            projectsNode.Name = othersCode;


            ToolStripItem addProject = projectsNode.ContextMenuStrip.Items.Add("Add project");
            addProject.Click += (o, e) =>
                {
                    var dialog = new AddOrEditProject(mainDatabase.mainOrganization, edit: false);
                    dialog.ShowDialog();
                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

            foreach (Project project in currentOrganization.Projects)
            {
                TreeNode cProjectNode = projectsNode.Nodes.Add(project.Subject);
                cProjectNode.ContextMenuStrip = new ContextMenuStrip();
                cProjectNode.Name = projectCode;
                cProjectNode.Tag = (object)project;

                ToolStripItem editProject = cProjectNode.ContextMenuStrip.Items.Add("Edit project");
                editProject.Tag = (object)project;

                editProject.Click += (o, e) =>
                    {
                        Project target = (Project)((ToolStripItem)o).Tag;
                        var dialog = new AddOrEditProject(mainDatabase.mainOrganization, true, target);

                        dialog.ShowDialog();
                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };

                ToolStripItem removeProject = cProjectNode.ContextMenuStrip.Items.Add("Remove project");
                removeProject.Tag = (object)project;

                removeProject.Click += (o, e) =>
                {
                    Project target = (Project)((ToolStripItem)o).Tag;

                    foreach (Worker worker in target.Department.Workers)
                    {
                        if (worker.CurrentProject == target)
                            worker.CurrentProject = null;
                    }

                    target.Department.Projects.Remove(target);
                    mainDatabase.mainOrganization.Projects.Remove(target);

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

                ToolStripItem reportProject = cProjectNode.ContextMenuStrip.Items.Add("View project report");
                reportProject.Tag = (object)project;

                reportProject.Click += (s, a) =>
                {
                    Project target = (Project)((ToolStripItem)s).Tag;

                    ProjectReportForm projectReportForm = new ProjectReportForm(currentOrganization, target);
                    projectReportForm.Show();
                };


            }

            #endregion

            #region Offices
            TreeNode officeNode = mainNode.Nodes.Add("Offices");
            officeNode.Expand();
            officeNode.ContextMenuStrip = new ContextMenuStrip();
            officeNode.Name = officeCode;

            ToolStripItem addOffice = officeNode.ContextMenuStrip.Items.Add("Add office");
            addOffice.Click += (o, e) =>
                {
                    var dialog = new AddOrEditOffice(mainDatabase.mainOrganization, false);
                    dialog.ShowDialog();

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

            foreach (Office office in currentOrganization.Offices)
            {
                TreeNode cOfficeNode = officeNode.Nodes.Add(office.Name.ToString());
                cOfficeNode.ContextMenuStrip = new ContextMenuStrip();
                cOfficeNode.Name = officeCode;
                cOfficeNode.Tag = (object)office;

                ToolStripItem editOffice = cOfficeNode.ContextMenuStrip.Items.Add("Edit office");
                editOffice.Tag = (object)office;
                editOffice.Click += (o, e) =>
                    {
                        Office target = (Office)((ToolStripItem)o).Tag;
                        var dialog = new AddOrEditOffice(mainDatabase.mainOrganization, edit: true,
                            aOffice: target);

                        dialog.ShowDialog();
                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };

                ToolStripItem removeOffice = cOfficeNode.ContextMenuStrip.Items.Add("Remove office");
                removeOffice.Tag = (object)office;
                removeOffice.Click += (o, e) =>
                {
                    Office target = (Office)((ToolStripItem)o).Tag;

                    foreach (Worker worker in target.HostDepartment.Workers)
                    {
                        if (worker.Office == target)
                            worker.Office = null;
                    }
                    target.HostDepartment.Offices.Remove(target);
                    mainDatabase.mainOrganization.Offices.Remove(target);

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

                TreeNode cTPNode = cOfficeNode.Nodes.Add("Telephone Posts");
                cTPNode.Name = othersCode;
                cTPNode.ContextMenuStrip = new ContextMenuStrip();

                ToolStripItem addTP = cTPNode.ContextMenuStrip.Items.Add("Add telephone post");
                addTP.Tag = (object)office;
                addTP.Click += (o, e) =>
                    {
                        Office target = (Office)((ToolStripItem)o).Tag;

                        var dialog = new AddOrEditTelephonePost(target, false);

                        dialog.ShowDialog();
                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };

                for (int i = 0; i < office.TelephonePost.Count; ++ i)
                {
                    TreeNode telephoneNode = cTPNode.Nodes.Add(office.TelephonePost[i].TelephoneNumber.Number);
                    telephoneNode.Name = othersCode;

                    telephoneNode.ContextMenuStrip = new ContextMenuStrip();
                    ToolStripItem editTP = telephoneNode.ContextMenuStrip.Items.Add("Edit telephone post");
                    editTP.Name = i.ToString();
                    editTP.Tag = (object)office;

                    editTP.Click += (o, e) =>
                        {
                            Office target = (Office) ((ToolStripItem)o).Tag;
                            var dialog = new AddOrEditTelephonePost(target, true, target.TelephonePost[
                                int.Parse(((ToolStripItem)o).Name)]);

                            dialog.ShowDialog();
                            Dictionary<string, bool> state = SaveTreeState(MainView);
                            PopulateTreeView();
                            RestoreTreeState(MainView, state);
                        };

                    ToolStripItem removeTP = telephoneNode.ContextMenuStrip.Items.Add("Remove telephone post");
                    removeTP.Name = i.ToString();
                    removeTP.Tag = (object)office;

                    removeTP.Click += (o, e) =>
                    {
                        Office target = (Office)((ToolStripItem)o).Tag;
                        target.TelephonePost.RemoveAt(int.Parse(((ToolStripItem)o).Name));

                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };
                }
            }

            #endregion
 
            #region Workers
            TreeNode workersNode = mainNode.Nodes.Add("Workers");
            workersNode.ContextMenuStrip = new ContextMenuStrip();
            workersNode.Name = othersCode;

            ToolStripItem addWorker = workersNode.ContextMenuStrip.Items.Add("Add worker");
            addWorker.Click += (o, e) =>
                {
                    var dialog = new AddOrEditWorker(mainDatabase.mainOrganization, false);
                    dialog.ShowDialog();

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

            foreach (Worker worker in mainDatabase.mainOrganization.Workers)
            {
                TreeNode workerNode = workersNode.Nodes.Add(worker.Name);
                workerNode.ContextMenuStrip = new ContextMenuStrip();
                workerNode.Name = workerCode;
                workerNode.Tag = (object)worker;

                ToolStripItem editWorker = workerNode.ContextMenuStrip.Items.Add("Edit worker");
                editWorker.Tag = (object)worker;
                editWorker.Click += (o, e) =>
                    {
                        Worker target = (Worker)((ToolStripItem)o).Tag;

                        var dialog = new AddOrEditWorker(mainDatabase.mainOrganization, true,
                            target);
                        dialog.ShowDialog();

                        Dictionary<string, bool> state = SaveTreeState(MainView);
                        PopulateTreeView();
                        RestoreTreeState(MainView, state);
                    };

                ToolStripItem reassignWorker = workerNode.ContextMenuStrip.Items.Add("Reassign worker");
                reassignWorker.Tag = (object)worker;
                reassignWorker.Click += (o, e) =>
                {
                    Worker target = (Worker)((ToolStripItem)o).Tag;

                    var dialog = new ReassignWorker(mainDatabase.mainOrganization, target);
                    dialog.ShowDialog();

                    Dictionary<string, bool> state = SaveTreeState(MainView);
                    PopulateTreeView();
                    RestoreTreeState(MainView, state);
                };

                #region worker job history

                TreeNode historyNode = workerNode.Nodes.Add("Job history");
                foreach (Worker.JobEntry entry in worker.JobHistory)
                {
                    TreeNode entryNode = historyNode.Nodes.Add(entry.Position);
                    entryNode.Name = jobHistoryCode;
                    entryNode.Tag = (object)entry;
                }

                #endregion
            }

            #endregion

            MainView.Nodes.Add(mainNode);
        }

        private void MainView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                listBoxDetails.Items.Clear();
                TreeNode node = MainView.SelectedNode;
                switch (node.Name)
                {
                    case officeCode:
                        Office office = (Office)node.Tag;
                        listBoxDetails.Items.Add("ID: " + office.ID.ToString());
                        listBoxDetails.Items.Add("Name: " + office.Name);
                        listBoxDetails.Items.Add("Area: " + office.Area.AreaInQuadratMeter.ToString());
                        if (office.HostDepartment == null)
                            listBoxDetails.Items.Add("Host department: not assigned");
                        else listBoxDetails.Items.Add("Host department: " + office.HostDepartment.Name);

                        break;

                    case projectCode:
                        Project project = (Project)node.Tag;
                        listBoxDetails.Items.Add("ID: " + project.ID);
                        listBoxDetails.Items.Add("Subject: " + project.Subject);
                        listBoxDetails.Items.Add("Budget: " + project.Budget.ToString());
                        if (project.Department == null)
                            listBoxDetails.Items.Add("Host department: not assigned");
                        else listBoxDetails.Items.Add("Host department: " + project.Department.Name);
                        listBoxDetails.Items.Add("Note: " + project.Note);

                        break;

                    case departmentCode:
                        Department department = (Department)node.Tag;
                        listBoxDetails.Items.Add("ID: " + department.ID);
                        listBoxDetails.Items.Add("Name: " + department.Name);
                        listBoxDetails.Items.Add("Budget: " + department.Budget.ToString());
                        if (department.Manager == null)
                            listBoxDetails.Items.Add("Manager: not assigned");
                        else listBoxDetails.Items.Add("Manager: " + department.Manager.Name);

                        break;

                    case workerCode:
                        Worker worker = (Worker)node.Tag;
                        listBoxDetails.Items.Add("ID: " + worker.ID);
                        listBoxDetails.Items.Add("Name: " + worker.Name);

                        if (worker.CurrentProject == null)
                            listBoxDetails.Items.Add("Project: not assigned");
                        else listBoxDetails.Items.Add("Project: " + worker.CurrentProject.Subject);
                        
                        if (worker.Department == null)
                            listBoxDetails.Items.Add("Department: not assigned");
                        else listBoxDetails.Items.Add("Department: " + worker.Department.Name);

                        if (worker.TelephonePost == null)
                            listBoxDetails.Items.Add("Telephone: not assigned");
                        else listBoxDetails.Items.Add("Telephone: " + worker.TelephonePost.TelephoneNumber.Number);

                        listBoxDetails.Items.Add("Hire date: " + worker.CurrentJob.HireDate);
                        listBoxDetails.Items.Add("Payment: " + worker.CurrentJob.Payment);
                        listBoxDetails.Items.Add("Position: " + worker.CurrentJob.Position);

                        break;

                    case jobHistoryCode:
                        Worker.JobEntry entry = (Worker.JobEntry)node.Tag;

                        listBoxDetails.Items.Add("Position: " + entry.Position);
                        listBoxDetails.Items.Add("Payment: " + entry.Payment);
                        listBoxDetails.Items.Add("Hire date: " + entry.HireDate);

                        break;
                }
            }
            catch (Exception exc)
            {
                //Do nothing. It crashed because of too fast switching.
            }
        }

        private void SaveRecursive(TreeNode node, ref Dictionary<string, bool> treeState)
        {
            try
            {
                treeState.Add(node.FullPath, node.IsExpanded);
            }
            catch (Exception exc)
            {
                //holy shit.. don't ask why that's here
            }
            foreach (TreeNode tn in node.Nodes)
                SaveRecursive(tn, ref treeState);
        }

        private Dictionary<string, bool> SaveTreeState(TreeView tree)
        {
            Dictionary<string, bool> nodeStates = new Dictionary<string, bool>();
            SaveRecursive(tree.Nodes[0], ref nodeStates);

            return nodeStates;
        }

        private void RestoreRecursive(TreeNode node, ref Dictionary<string, bool> treeState)
        {
            if (treeState.ContainsKey(node.FullPath))
            {
                if (treeState[node.FullPath])
                    node.Expand();
                else node.Collapse();
            }

            foreach (TreeNode tn in node.Nodes)
                RestoreRecursive(tn, ref treeState);
        }

        private void RestoreTreeState(TreeView tree, Dictionary<string, bool> treeState)
        {
            RestoreRecursive(tree.Nodes[0], ref treeState);
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainDatabase = new Database();
            mainDatabase.mainOrganization = new Organization();
            Organization currentOrganization = mainDatabase.mainOrganization;
            currentOrganization.Departments = new List<Department>();
            currentOrganization.Offices = new List<Office>();
            currentOrganization.Projects = new List<Project>();
            currentOrganization.Workers = new List<Worker>();

            PopulateTreeView();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = fileFilter;
            DialogResult result = openFileDialog.ShowDialog();

            if (result != DialogResult.OK)
                return;

            mainDatabase = new Database();
            try
            {
                mainDatabase.LoadDatabase(openFileDialog.FileName);
            }
            catch (Exception exc)
            {
                MessageBox.Show(Database.ErrorOpeningMessage);
                Console.WriteLine(exc.Message);
                return;
            }

            DoIndexingRelocation();
            PopulateTreeView();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = fileFilter;
            DialogResult result = saveFileDialog.ShowDialog();

            if (result != DialogResult.OK)
                return;

            mainDatabase.SaveDatabase(saveFileDialog.FileName);
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mainDatabase == null)
                return;

            var dialog = new AddOrEditDepartment(mainDatabase.mainOrganization, false);
            
            dialog.ShowDialog();
            Dictionary<string, bool> state = SaveTreeState(MainView);
            PopulateTreeView();
            RestoreTreeState(MainView, state);
        }

        private void addToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (mainDatabase == null)
                return;

            var dialog = new AddOrEditProject(mainDatabase.mainOrganization, false);

            dialog.ShowDialog();
            Dictionary<string, bool> state = SaveTreeState(MainView);
            PopulateTreeView();
            RestoreTreeState(MainView, state);
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mainDatabase == null)
                return;

            var dialog = new AddOrEditOffice(mainDatabase.mainOrganization, false);

            dialog.ShowDialog();
            Dictionary<string, bool> state = SaveTreeState(MainView);
            PopulateTreeView();
            RestoreTreeState(MainView, state);
        }

        private void addToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (mainDatabase == null)
                return;

            var dialog = new AddOrEditWorker(mainDatabase.mainOrganization, false);

            dialog.ShowDialog();
            Dictionary<string, bool> state = SaveTreeState(MainView);
            PopulateTreeView();
            RestoreTreeState(MainView, state);
        }

    }
}
